﻿using System.Collections;
using System.Collections.Generic;

using Entitas;

using UnityEngine;

namespace Internal
{
    /// <summary>
    /// 添加图片系统
    /// </summary>
    public class AddSpriteSystem : ReactiveSystem<GameEntity>
    {

        public AddSpriteSystem(Contexts contexts) : base(contexts.game)
        {

        }

        protected override void Execute(List<GameEntity> entities)
        {
            foreach (GameEntity entity in entities)
            {
                Transform trans = entity.internalView.viewTrans;
                SpriteRenderer sprite = trans.GetComponent<SpriteRenderer>();
                if (sprite == null)
                {
                    sprite = trans.gameObject.AddComponent<SpriteRenderer>();
                }
                sprite.sprite = Resources.Load<Sprite>(entity.internalSprite.spriteName);
            }
        }

        protected override bool Filter(GameEntity entity)
        {
            return entity.hasInternalSprite && entity.hasInternalView;
        }

        protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
        {
            return context.CreateCollector(GameMatcher.InternalSprite);
        }
    }
}

